package com.hup.vd.view.customView;

import com.hup.utils.commons.DateBuilder;
import com.hup.utils.commons.exception.AppExceptionHandler;
import com.hup.utils.commons.exception.ErrInfo;
import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.swing.ComponentUtil;
import com.hup.utils.swing.customView.HScrollPanel;
import com.hup.utils.swing.customView.ImageLabel;
import com.hup.utils.swing.customView.ViewText;
import com.hup.vd.model.config.TaskInfo;
import lombok.Getter;
import org.springframework.util.StringUtils;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;

/**
 * @author hugan
 * @date 2022/1/11
 */
public class TaskInfoPanel extends JPanel {
    public JPanel root;
    public ViewText txtUid;
    public JLabel labUrl;
    public ViewText txtUrl;
    public ViewText txtFilePath;
    public ViewText txtCreateTime;
    public ViewText txtFinishTime;
    public JTextArea txtMsg;
    public HScrollPanel spMsg;
    public ImageLabel imgProcess;
    public JButton btnEdit;

    @Getter
    private TaskInfo task;
    //解决[定时更新ui造成text选中困难的bug];异味/扭曲写法,不深究
    private Throwable lastUpdateErr;

    public TaskInfoPanel() {
        $$$setupUI$$$();
        txtMsg.setFont(new Font("宋体", Font.PLAIN, (int) (12 * LookThemeUtil.getScreenScale())));
        labUrl.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnEdit.setVisible(false);
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        root.setLayout(new GridBagLayout());
        root.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JLabel label1 = new JLabel();
        label1.setText("任务id");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        root.add(label1, gbc);
        labUrl = new JLabel();
        labUrl.setHorizontalAlignment(4);
        labUrl.setText("<html>　 url\n<br>(<span style=\"color:blue;text-decoration:underline\">打开</span>)\n</html>");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.EAST;
        root.add(labUrl, gbc);
        final JLabel label2 = new JLabel();
        label2.setText("保存路径");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.EAST;
        root.add(label2, gbc);
        final JLabel label3 = new JLabel();
        label3.setText("创建时间");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.EAST;
        root.add(label3, gbc);
        final JLabel label4 = new JLabel();
        label4.setText("完成时间");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.EAST;
        root.add(label4, gbc);
        txtUid = new ViewText();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(0, 5, 0, 0);
        root.add(txtUid, gbc);
        txtUrl = new ViewText();
        txtUrl.setLineWrap(true);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(0, 5, 0, 0);
        root.add(txtUrl, gbc);
        txtFilePath = new ViewText();
        txtFilePath.setLineWrap(true);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(0, 5, 0, 0);
        root.add(txtFilePath, gbc);
        txtCreateTime = new ViewText();
        txtCreateTime.setLineWrap(true);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(0, 5, 0, 0);
        root.add(txtCreateTime, gbc);
        txtFinishTime = new ViewText();
        txtFinishTime.setLineWrap(true);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(0, 5, 0, 0);
        root.add(txtFinishTime, gbc);
        spMsg = new HScrollPanel();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.gridwidth = 3;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(spMsg, gbc);
        txtMsg = new JTextArea();
        txtMsg.setEditable(false);
        txtMsg.setVisible(false);
        spMsg.setViewportView(txtMsg);
        imgProcess = new ImageLabel();
        imgProcess.setMinimumSize(new Dimension(0, 100));
        imgProcess.setPreferredSize(new Dimension(0, 100));
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(imgProcess, gbc);
        btnEdit = new JButton();
        btnEdit.setText("编辑");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        root.add(btnEdit, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return root;
    }

    private void resetView() {
        txtUid.setText(null);
        txtUrl.setText(null);
        txtFilePath.setText(null);
        txtCreateTime.setText(null);
        txtFinishTime.setText(null);
        imgProcess.setBackgroundImage(null);
        imgProcess.setVisible(false);
        txtMsg.setVisible(false);
        txtMsg.setText(null);
        lastUpdateErr = null;
        btnEdit.setVisible(false);
    }

    /**
     * 调用时机:用户点击时,
     */
    public void setTask(TaskInfo task) {
        this.task = task;
        if (task == null) {
            resetView();
            return;
        }
        btnEdit.setVisible(true);
        txtUid.setText(task.getUid());
        if (StringUtils.hasText(task.getHtmlUrl())) {
            txtUrl.setText(task.getHtmlUrl() + "\n" + task.getHtmlTitle() + "\n" + task.getUrl());
        } else {
            txtUrl.setText(task.getUrl());
        }
        txtFilePath.setText(task.getFilePath());
        txtCreateTime.setText(DateBuilder.ymdHms(task.getCreateTime()));
        txtFinishTime.setText(DateBuilder.ymdHms(task.getFinishTime()));
        Throwable e = task.getTaskErr();
        if (lastUpdateErr != e) {
            if (e == null) {
                txtMsg.setVisible(false);
                txtMsg.setText("");
            } else {
                txtMsg.setVisible(true);
                ErrInfo errInfo = AppExceptionHandler.handle(null, e, false, false, null);
                txtMsg.setText(errInfo.toString());
                ComponentUtil.scrollPaneTo(spMsg, false, true);
            }
            lastUpdateErr = e;
        }
        updateProgressImage();
    }

    /**
     * 调用时机:用户点击时,每秒更新进度
     */
    public void updateProgressImage() {
        if (task == null) return;
        BufferedImage progressImage = task.getProgressImage();
        imgProcess.setVisible(progressImage != null);
        imgProcess.setBackgroundImage(progressImage);
        repaint();
    }

    private void createUIComponents() {
        root = this;
    }
}
